/*******************************************************************************
 * Copyright (c) 2014 Steffen Mikkelsen
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Steffen Mikkelsen - initial API and implementation
 ******************************************************************************/
package com.subsecret.secureconnection.streamtools;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class ForwardDataThread extends Thread {
    
    InputStream inputStream;
    OutputStream outputStream;
    
    public ForwardDataThread(InputStream inputStream, OutputStream outputStream) {
        this.inputStream = inputStream;
        this.outputStream = outputStream;
    }
    
    @Override
    public void run() {
        byte[] buf = new byte[32768];
        try {
            while (true) {
                int read = inputStream.read(buf);
                if (read == -1) {
                    outputStream.close();
                    break;
                }
                outputStream.write(buf, 0, read);
                outputStream.flush();
            }
        }
        catch (IOException e) {
            StreamTools.closeWithoutException(outputStream);
            StreamTools.closeWithoutException(inputStream);
        }
    }
    
}
